/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.MD5;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UploadWhiteBoardImage extends AbstractAction
/*     */ {
/*  29 */   private static Logger logger = Logger.getLogger(UploadWhiteBoardImage.class
/*  30 */     .getName());
/*     */ 
/*  32 */   private String path = "";
/*     */ 
/*     */   public UploadWhiteBoardImage(AbstractHttpServlet servlet) {
/*  35 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public UploadWhiteBoardImage(AbstractHttpServlet servlet, String uploadPath) {
/*  39 */     super(servlet);
/*  40 */     this.path = uploadPath;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  47 */     if (!ServletFileUpload.isMultipartContent(request)) {
/*  48 */       throw new IllegalArgumentException(
/*  49 */         "Request is not multipart, please 'multipart/form-data' enctype for your form.");
/*     */     }
/*     */ 
/*  52 */     DiskFileItemFactory fac = new DiskFileItemFactory();
/*  53 */     ServletFileUpload upload = new ServletFileUpload(fac);
/*  54 */     upload.setHeaderEncoding("utf-8");
    List fileList;
/*     */     try
/*     */     {
/*  57 */       fileList = upload.parseRequest(request);
/*     */     }
/*     */     catch (FileUploadException e)
/*     */     {
/*     */
/*  59 */       logger.warn("upload image failed", e);
/*  60 */       showJSONResult(response, null);
/*     */       return;
/*     */     }
/*  64 */     Iterator it = fileList.iterator();
/*  65 */     String imgPath = "";
/*  66 */     while (it.hasNext()) {
/*  67 */       FileItem item = (FileItem)it.next();
/*  68 */       if (!item.isFormField())
/*     */       {
/*  72 */         String fileName = item.getName();
/*  73 */         if ((fileName != null) && (!fileName.trim().equals("")))
/*     */         {
/*  77 */           String extName = "";
/*  78 */           if (fileName.lastIndexOf(".") >= 0) {
/*  79 */             extName = fileName.substring(fileName.lastIndexOf("."));
/*     */           }
/*     */ 
/*  82 */           if (!Util.isImageFile(extName, item.get())) {
/*  83 */             logger.warn("the file is not image");
/*  84 */             showJSONResult(response, null);
/*  85 */             return;
/*     */           }
/*  87 */           extName = extName.toLowerCase();
/*  88 */           imgPath = MD5.getMD5(fileName) + extName;
/*  89 */           File file = new File(this.path, imgPath);
/*     */           try {
/*  91 */             item.write(file);
/*     */           } catch (Exception e) {
/*  93 */             logger.warn("save image file failed", e);
/*  94 */             showJSONResult(response, null);
/*  95 */             return;
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*  98 */     showJSONResult(response, imgPath);
/*     */   }
/*     */ 
/*     */   private void showJSONResult(HttpServletResponse response, String filePath) throws IOException
/*     */   {
/* 103 */     response.setContentType("text/html;charset=utf-8");
/* 104 */     PrintWriter out = response.getWriter();
/* 105 */     JSONObject jsonObject = new JSONObject();
/* 106 */     if ((filePath != null) && (filePath.trim().length() > 0)) {
/* 107 */       jsonObject.put("file", filePath);
/* 108 */       jsonObject.put("error", "0");
/*     */     } else {
/* 110 */       jsonObject.put("error", "1");
/*     */     }
/* 112 */     out.println(jsonObject);
/* 113 */     out.flush();
/* 114 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.UploadWhiteBoardImage
 * JD-Core Version:    0.6.1
 */